home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 366_01 / ue311c1.arc / ANSI.C < prev    next >
C/C++ Source or Header  |  1991-10-11  |  13KB  |  581 lines

  1. /*
  2.  * The routines in this file provide support for ANSI style terminals
  3.  * over a serial line. The serial I/O services are provided by routines in
  4.  * termio.c. It compiles into nothing if not an ANSI device.
  5.  *
  6.  * 12-Dec-1989    KAM
  7.  * If we're on VMS, leave the keypad turned on if the terminal is set to
  8.  * application keypad mode.
  9.  */
  10.  
  11. #define    termdef    1            /* don't define term external */
  12.  
  13. #include        <stdio.h>
  14. #include    "estruct.h"
  15. #include    "eproto.h"
  16. #include        "edef.h"
  17. #include    "elang.h"
  18.  
  19. #if     ANSI
  20.  
  21. #if    PROTO
  22. int PASCAL NEAR fnclabel(int f, int n);
  23. int PASCAL NEAR readparam( int *v);
  24. void PASCAL NEAR dobbnmouse(void);
  25. void PASCAL NEAR docsi( int oh);
  26. void PASCAL NEAR ttputs(char *string);
  27. #else
  28. int PASCAL NEAR fnclabel();
  29. int PASCAL NEAR readparam();
  30. void PASCAL NEAR dobbnmouse();
  31. void PASCAL NEAR docsi();
  32. void PASCAL NEAR ttputs();
  33. #endif
  34.  
  35. #if VMS
  36. #include ttdef
  37. #include tt2def
  38.  
  39. /*
  40.     This structure, along with ttdef.h, is good for manipulating
  41.     terminal characteristics.
  42. */
  43. typedef struct
  44. {/* Terminal characteristics buffer */
  45.     unsigned char class, type;
  46.     unsigned short width;
  47.     unsigned tt1 : 24;
  48.     unsigned char page;
  49.     unsigned long tt2;
  50. } TTCHAR;
  51. extern NOSHARE TTCHAR orgchar;            /* Original characteristics */
  52. #endif
  53.  
  54. #define NROW    25                      /* Screen size.                 */
  55. #define NCOL    80                      /* Edit if you want to.         */
  56. #define    NPAUSE    100            /* # times thru update to pause */
  57. #define    MARGIN    8            /* size of minimim margin and    */
  58. #define    SCRSIZ    64            /* scroll size for extended lines */
  59. #define BEL     0x07                    /* BEL character.               */
  60. #define ESC     0x1B                    /* ESC character.               */
  61.  
  62. /* Forward references.          */
  63. extern int PASCAL NEAR ansimove();
  64. extern int PASCAL NEAR ansieeol();
  65. extern int PASCAL NEAR ansieeop();
  66. extern int PASCAL NEAR ansibeep();
  67. extern int PASCAL NEAR ansiopen();
  68. extern int PASCAL NEAR ansirev();
  69. extern int PASCAL NEAR ansiclose();
  70. extern int PASCAL NEAR ansikopen();
  71. extern int PASCAL NEAR ansikclose();
  72. extern int PASCAL NEAR ansicres();
  73. extern int PASCAL NEAR ansiparm();
  74. extern int PASCAL NEAR ansigetc();
  75.  
  76. #if    COLOR
  77. extern int PASCAL NEAR ansifcol();
  78. extern int PASCAL NEAR ansibcol();
  79.  
  80. static int cfcolor = -1;    /* current forground color */
  81. static int cbcolor = -1;    /* current background color */
  82.  
  83. #if    AMIGA
  84. /* Apparently the AMIGA does not follow the ANSI standards as
  85.  * regards to colors....maybe because of the default pallette
  86.  * settings?  Color translation table needed.
  87.  */
  88.  
  89. int coltran[16] = {2,  3,  5,  7, 0,  4,  6,  1,
  90.            8, 12, 10, 14, 9, 13, 11, 15};
  91. #endif
  92. #endif
  93.  
  94. /*
  95.  * Standard terminal interface dispatch table. Most of the fields point into
  96.  * "termio" code.
  97.  */
  98. NOSHARE TERM term    = {
  99.     NROW-1,
  100.         NROW-1,
  101.         NCOL,
  102.         NCOL,
  103.     0, 0,
  104.     MARGIN,
  105.     SCRSIZ,
  106.     NPAUSE,
  107.         ansiopen,
  108.         ansiclose,
  109.     ansikopen,
  110.     ansikclose,
  111.         ansigetc,
  112.         ttputc,
  113.         ttflush,
  114.         ansimove,
  115.         ansieeol,
  116.         ansieeop,
  117.         ansieeop,
  118.         ansibeep,
  119.     ansirev,
  120.     ansicres
  121. #if    COLOR
  122.     , ansifcol,
  123.     ansibcol
  124. #endif
  125. };
  126.  
  127. #if    COLOR
  128. PASCAL NEAR ansifcol(color)        /* set the current output color */
  129.  
  130. int color;    /* color to set */
  131.  
  132. {
  133.     if (color == cfcolor)
  134.         return;
  135.     ttputc(ESC);
  136.     ttputc('[');
  137. #if    AMIGA
  138.     ansiparm(coltran[color]+30);
  139. #else
  140.     ansiparm(color+30);
  141. #endif
  142.     ttputc('m');
  143.     cfcolor = color;
  144. }
  145.  
  146. PASCAL NEAR ansibcol(color)        /* set the current background color */
  147.  
  148. int color;    /* color to set */
  149.  
  150. {
  151.     if (color == cbcolor)
  152.         return;
  153.     ttputc(ESC);
  154.     ttputc('[');
  155. #if    AMIGA
  156.     ansiparm(coltran[color]+40);
  157. #else
  158.     ansiparm(color+40);
  159. #endif
  160.     ttputc('m');
  161.         cbcolor = color;
  162. }
  163. #endif
  164.  
  165. PASCAL NEAR ansimove(row, col)
  166. {
  167.         ttputc(ESC);
  168.         ttputc('[');
  169.         ansiparm(row+1);
  170.         ttputc(';');
  171.         ansiparm(col+1);
  172.         ttputc('H');
  173. }
  174.  
  175. PASCAL NEAR ansieeol()
  176. {
  177.         ttputc(ESC);
  178.         ttputc('[');
  179.         ttputc('K');
  180. }
  181.  
  182. PASCAL NEAR ansieeop()
  183. {
  184. #if    COLOR
  185.     ansifcol(gfcolor);
  186.     ansibcol(gbcolor);
  187. #endif
  188.         ttputc(ESC);
  189.         ttputc('[');
  190.         ttputc('J');
  191. }
  192.  
  193. PASCAL NEAR ansirev(state)        /* change reverse video state */
  194.  
  195. int state;    /* TRUE = reverse, FALSE = normal */
  196.  
  197. {
  198. #if    COLOR
  199.     int ftmp, btmp;        /* temporaries for colors */
  200. #endif
  201.  
  202.     ttputc(ESC);
  203.     ttputc('[');
  204.     ttputc(state ? '7': '0');
  205.     ttputc('m');
  206. #if    COLOR
  207.     if (state == FALSE) {
  208.         ftmp = cfcolor;
  209.         btmp = cbcolor;
  210.         cfcolor = -1;
  211.         cbcolor = -1;
  212.         ansifcol(ftmp);
  213.         ansibcol(btmp);
  214.     }
  215. #endif
  216. }
  217.  
  218. PASCAL NEAR ansicres()    /* change screen resolution */
  219.  
  220. {
  221.     return(TRUE);
  222. }
  223.  
  224. #if    PROTO
  225. PASCAL NEAR spal(char *dummy)        /* change pallette settings */
  226. #else
  227. PASCAL NEAR spal(dummy)        /* change pallette settings */
  228.  
  229. char *dummy;
  230. #endif
  231.  
  232. {
  233.     /* none for now */
  234. }
  235.  
  236. PASCAL NEAR ansibeep()
  237. {
  238.         ttputc(BEL);
  239.         ttflush();
  240. }
  241.  
  242. PASCAL NEAR ansiparm(n)
  243. register int    n;
  244. {
  245.         register int q,r;
  246.  
  247.         q = n/10;
  248.         if (q != 0) {
  249.         r = q/10;
  250.         if (r != 0) {
  251.             ttputc((r%10)+'0');
  252.         }
  253.         ttputc((q%10) + '0');
  254.         }
  255.         ttputc((n%10) + '0');
  256. }
  257.  
  258. PASCAL NEAR ansiopen()
  259. {
  260. #if     V7 | USG | HPUX | BSD | SUN | XENIX
  261.         register char *cp;
  262.         char *getenv();
  263.  
  264.         if ((cp = getenv("TERM")) == NULL) {
  265.                 puts(TEXT4);
  266. /*                   "Shell variable TERM not defined!" */
  267.                 meexit(1);
  268.         }
  269.         if (strcmp(cp, "vt100") != 0 && strcmp(cp,"vt200") != 0 &&
  270.                     strcmp(cp,"vt300") != 0) {
  271.                 puts(TEXT5);
  272. /*                   "Terminal type not 'vt100'!" */
  273.                 meexit(1);
  274.         }
  275. #endif
  276. #if    MOUSE & ( V7 | USG | HPUX | BSD | SUN | XENIX | VMS)
  277. /*
  278.     If this is an ansi terminal of at least DEC level
  279.     2 capability, some terminals of this level, such as the "Whack"
  280.     emulator, the VWS terminal emulator, and some versions of XTERM,
  281.     support access to the workstation mouse via escape sequences.  In
  282.     addition, any terminal that conforms to level 2 will, at very least,
  283.     IGNORE the escape sequences for the mouse.
  284. */
  285.     {
  286.     char *s;
  287.  
  288.     s = getenv( "MICROEMACS$MOUSE_ENABLE");
  289.     if( !s) s = "\033[1)u\033[1;3'{\033[1;2'z";
  290.     ttputs( s);
  291.     }
  292. #endif
  293.     strcpy(sres, "NORMAL");
  294.     revexist = TRUE;
  295.         ttopen();
  296.  
  297. #if    KEYPAD
  298.     ttputc(ESC);
  299.     ttputc('=');
  300. #endif
  301. }
  302.  
  303. PASCAL NEAR ansiclose()
  304.  
  305. {
  306. #if    COLOR
  307.     ansifcol(7);
  308.     ansibcol(0);
  309. #endif
  310. #if    MOUSE & ( V7 | USG | HPUX | BSD | SUN | XENIX | VMS)
  311.     {
  312.         char *s;
  313.  
  314.         s = getenv( "MICROEMACS$MOUSE_DISABLE");
  315.  
  316.         if( !s)        /* Regular DEC workstation */
  317.             s = "\033[0'{\033[0;0'z";
  318.         ttputs( s);
  319.     }
  320. #endif
  321. #if    KEYPAD
  322. #if     VMS
  323.     if ((orgchar.tt2 & TT2$M_APP_KEYPAD)==0)
  324. #endif
  325.     {
  326.         ttputc(ESC);
  327.         ttputc('>');
  328.     }
  329. #endif
  330.     ttclose();
  331. }
  332.  
  333. PASCAL NEAR ansikopen()    /* open the keyboard (a noop here) */
  334.  
  335. {
  336. }
  337.  
  338. PASCAL NEAR ansikclose()    /* close the keyboard (a noop here) */
  339.  
  340. {
  341. }
  342.  
  343. #if     V7 | USG | HPUX | BSD | SUN | XENIX | VMS
  344. /***
  345.  *  ttputs  -  Send a string to ttputc
  346.  *
  347.  *  Nothing returned
  348.  ***/
  349. void PASCAL NEAR ttputs(string)
  350. char * string;                /* String to write        */
  351. {
  352.     if (string)
  353.         while (*string != '\0')
  354.             ttputc(*string++);
  355. }
  356.  
  357. /*
  358.     On the "real" ansi terminals, used on "mainframe" type
  359.     terminal/CPU connections of the above operating systems, we do
  360.     conversion from VT100/VT200 style function keys into the Emacs
  361.     standard key sequence form.
  362. */
  363. static unsigned char inbuffer[ 10];
  364. static int inpos=0;
  365. static int inlen=0;
  366.  
  367. NOSHARE int mouserow, mousecol;
  368.  
  369. int PASCAL NEAR readparam( v)    /* Read an ansi parameter */
  370. int *v;    /* Place to put parameter value */
  371. {
  372.     int ch;
  373.  
  374.     *v = 0;
  375.     for(;;)
  376.     { /* Read in a number */
  377.        ch = ttgetc();
  378.     if( ch>='0' && ch<='9') *v = 10 * *v + (ch - '0');
  379.     else return( ch);
  380.     }
  381. }
  382.  
  383. /*
  384.  * Handle the CSI (<esc>[) and SS3 (<esc>O) sequences.
  385.  * Static arrays are set up to translate the ANSI escape sequence
  386.  * into the MicroE